<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use service\DataService;
use service\ToolsService;
use controller\BasicAdmin;
use think\Db;

/**
 * 统计管理
 * Class Sta
 * @package app\admin\controller
 * @author  
 * @date 2019/07/24
 */
class Sta extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'Area';
    public $article = 'Article';
    public $statistics = 'Statistics';
    public $category;
    public $statistic;
    public $areaList;

    public function _initialize()
    {
        parent::_initialize();
        $_areaList = $this->AR->order('area_sort asc, area_id asc')->select();
        $this->areaList = ToolsService::arr2table($_areaList, 'area_id', 'area_parent_id');

        //产品+分类
        $this->category = model('Category');
        // $this->categorys = $this->category->getLists(['parentid' => 1], 'sort desc', 'id,catname,module');
        $this->categorys1 = $this->category->getLists(['parentid' => 1], 'sort asc', 'id,catname,module');//课件
        $this->categorys2 = $this->category->getLists(['id' => 6], 'sort asc', 'id,catname,module');//新闻动态
        
        $this->columns = [
            ['id' => 0, 'catname' => '会员数', 'module' => 'member'],
            ['id' => 1, 'catname' => '传承人数', 'module' => 'client'],
            ['id' => 7, 'catname' => '特色诊疗', 'module' => 'characteristic'],
            ['id' => 8, 'catname' => '临床病案', 'module' => 'clinic'],
            ['id' => 9, 'catname' => '医药单方', 'module' => 'prescription'],
            ['id' => 10, 'catname' => '药品库', 'module' => 'drug'],
            ['id' => 11, 'catname' => '推广示范', 'module' => 'demonstration'],
        ];
        $this->cates = array_merge($this->columns, $this->categorys1, [['id' => 6, 'catname' => '新闻动态', 'module' => 'article']]);
        // $this->assign('categorys1', $this->categorys1);
        // halt($this->cates);
    }

    //图表-地区
    public function charts() {
        // $this->title = '图表统计';
        $map['area_parent_id'] = $this->aid;
        $areaData = $this->AR->order('area_sort asc, area_id asc')->where($map)->field('area_id,area_name')->select();
        // $diseases = Db::name('Disease')->order('sort asc, id asc')->where(['status' => 1])->field('id,title')->select();
        // $nations = Db::name('Nation')->order('sort asc, id asc')->where(['status' => 1])->field('id,title')->select();
        
        $dData = $setData = [];
        $xData1 = array_column($areaData, 'area_name');
        // $xData2 = array_column($diseases, 'title');
        // $xData3 = array_column($nations, 'title');
        $yData = array_column($this->cates, 'catname');
        $setData = $xData1;//数据集
        array_unshift($setData, 'num');
        $setData = [$setData];
        // halt($this->cates);
        foreach ($this->cates as $k => $v) {
            $vData = [$v['catname']];
            foreach ($areaData as $kk => $vv) {
                $vv['ids'] = _getAIDs($vv['area_id']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'aid' => ['in', $vv['ids']],
                ];
                if(in_array($v['id'], [2,3,4,5,6])){
                    $map['catid'] = $v['id'];
                }
                $proData = Db::name($v['module'])->where($map)->count();
                array_push($dData, [$kk, $k, $proData]);
                array_push($vData, $proData);
            }
            array_push($setData, $vData);
        }
        // halt($setData);
        $data = [
            'xData1' => $xData1,//x轴
            // 'xData2' => $xData2,//x轴
            // 'xData3' => $xData3,//x轴
            'yData' => $yData,//y轴
            'dData' => $dData,//第三维度数据值
            'setData' => $setData
        ];
        return $this->fetch('', $data);
    }

    //图表-疾病
    public function charts_disease() {
        $diseases = Db::name('Disease')->order('sort asc, id asc')->where(['status' => 1])->field('id,title')->select();
        $columns = [            
            ['id' => 1, 'catname' => '传承人数', 'module' => 'client'],
            ['id' => 7, 'catname' => '特色诊疗', 'module' => 'characteristic'],
            ['id' => 9, 'catname' => '医药单方', 'module' => 'prescription'],
            ['id' => 10, 'catname' => '药品库', 'module' => 'drug'],
        ];
        
        $dData = $setData = [];
        $xData2 = array_column($diseases, 'title');
        $yData = array_column($columns, 'catname');
        $setData = $xData2;//数据集
        array_unshift($setData, 'num');
        $setData = [$setData];
        // halt($columns);
        foreach ($columns as $k => $v) {
            $vData = [$v['catname']];
            foreach ($diseases as $kk => $vv) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'did' => $vv['id'],
                ];
                $proData = Db::name($v['module'])->where($map)->count();
                array_push($dData, [$kk, $k, $proData]);
                array_push($vData, $proData);
            }
            array_push($setData, $vData);
        }
        // halt($setData);
        $data = [
            'xData2' => $xData2,//x轴
            'yData' => $yData,//y轴
            'dData' => $dData,//第三维度数据值
            'setData' => $setData
        ];
        return $this->fetch('', $data);
    }

    //图表-民族
    public function charts_nation() {
        $nations = Db::name('Nation')->order('sort asc, id asc')->where(['status' => 1])->field('id,title')->select();
        $columns = [            
            ['id' => 1, 'catname' => '传承人数', 'module' => 'client'],
            ['id' => 7, 'catname' => '特色诊疗', 'module' => 'characteristic'],
            ['id' => 9, 'catname' => '医药单方', 'module' => 'prescription'],
            ['id' => 10, 'catname' => '药品库', 'module' => 'drug'],
        ];
        
        $dData = $setData = [];
        $xData3 = array_column($nations, 'title');
        $yData = array_column($columns, 'catname');
        $setData = $xData3;//数据集
        array_unshift($setData, 'num');
        $setData = [$setData];
        // halt($columns);
        foreach ($columns as $k => $v) {
            $vData = [$v['catname']];
            foreach ($nations as $kk => $vv) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'nid' => $vv['id'],
                ];
                $proData = Db::name($v['module'])->where($map)->count();
                array_push($dData, [$kk, $k, $proData]);
                array_push($vData, $proData);
            }
            array_push($setData, $vData);
        }
        // halt($setData);
        $data = [
            'xData3' => $xData3,//x轴
            'yData' => $yData,//y轴
            'dData' => $dData,//第三维度数据值
            'setData' => $setData
        ];
        return $this->fetch('', $data);
    }

    /**
     * 显示地区列表
     */
    public function index() {
        $this->title = '统计管理';
        // $sData = action('Admin/Domain/sData');
        // halt($sData);

        // $map['area_parent_id'] = $this->request->param('pid', 0); // 默认获取一级地区
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);

        // halt($this->statistic);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);

            //会员
            $members = Db::name('Member')->where(['status' => 1,'is_deleted' => 0,'aid' => ['in', $vo['ids']]])->select();
            $vo['member']['total'] = count($members);
            $vo['member']['catname'] = '会员';
            $vo['member']['url'] = url('/admin').'#/admin/member/index?spm=m-2-210';

            //传承人
            $clients = Db::name('Client')->where(['status' => 1,'is_deleted' => 0,'aid' => ['in', $vo['ids']]])->select();
            $vo['client']['catname'] = '传承人';
            $vo['client']['total'] = count($clients);
            $vo['client']['url'] = url('/admin').'#/admin/clientuser/index?spm=m-2-190';
            
            //诊疗技术
            $chas = Db::name('Characteristic')->where(['status' => 1,'aid' => ['in', $vo['ids']]])->select();
            $vo['cha']['total'] = count($chas);
            $vo['cha']['catname'] = '诊疗技术';
            $vo['cha']['url'] = url('/admin').'#/admin/characteristic/index?spm=m-206-200';

            //药品
            $drugs = Db::name('Drug')->where(['status' => 1,'aid' => ['in', $vo['ids']]])->select();
            $vo['drug']['catname'] = '药品';
            $vo['drug']['total'] = count($drugs);
            $vo['drug']['url'] = url('/admin').'#/admin/drug/index?spm=m-208-202';

            //新闻动态
            foreach ($this->categorys2 as $k => $v) {
                // halt($vo['ids']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $vo['ids']],
                ];
                $vo['news'][$v['id']]['catname'] = $v['catname'];
                $vo['news'][$v['id']]['total'] = Db::name($v['module'])->where($map)->count();
                $vo['news'][$v['id']]['url'] = url('/admin')."#/admin/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-166-178';
            }

            //课件
            foreach ($this->categorys1 as $k => $v) {
                // halt($vo['ids']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $vo['ids']],
                ];
                $vo['cate'][$v['id']]['catname'] = $v['catname'];
                $vo['cate'][$v['id']]['total'] = Db::name($v['module'])->where($map)->count();
                $vo['cate'][$v['id']]['url'] = url('/admin')."#/admin/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-166-178';
            }

            // halt($clients);
        }
    }

    public function _ajax_get_admin()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);

            //会员
            $members = Db::name('Member')->where(['status' => 1,'is_deleted' => 0,'aid' => ['in', $data['ids']]])->select();
            $datas[$key]['member']['total'] = count($members);
            $datas[$key]['member']['catname'] = '会员';
            $datas[$key]['member']['url'] = url('/admin').'#/admin/member/index?spm=m-2-210&aid='.$data['area_id'];

            //传承人
            $clients = Db::name('Client')->where(['status' => 1,'is_deleted' => 0,'aid' => ['in', $data['ids']]])->select();
            $datas[$key]['client']['catname'] = '传承人';
            $datas[$key]['client']['total'] = count($clients);
            $datas[$key]['client']['url'] = url('/admin').'#/admin/clientuser/index?spm=m-2-190&aid='.$data['area_id'];
            

            //诊疗技术
            $chas = Db::name('Characteristic')->where(['status' => 1,'aid' => ['in', $data['ids']]])->select();
            $datas[$key]['cha']['total'] = count($chas);
            $datas[$key]['cha']['catname'] = '诊疗技术';
            $datas[$key]['cha']['url'] = url('/admin').'#/admin/characteristic/index?spm=m-206-200&aid='.$data['area_id'];

            //药品
            $drugs = Db::name('Drug')->where(['status' => 1,'aid' => ['in', $data['ids']]])->select();
            $datas[$key]['drug']['catname'] = '药品';
            $datas[$key]['drug']['total'] = count($drugs);
            $datas[$key]['drug']['url'] = url('/admin').'#/admin/drug/index?spm=m-208-202&aid='.$data['area_id'];

            //新闻动态
            foreach ($this->categorys2 as $k => $v) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $data['ids']],
                ];
                $datas[$key]['news'][$k]['catname'] = $v['catname'];
                $datas[$key]['news'][$k]['total'] = Db::name($v['module'])->where($map)->count();
                $datas[$key]['news'][$k]['url'] = url('/admin')."#/admin/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-166-178';
            }

            //课件
            foreach ($this->categorys1 as $k => $v) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $data['ids']],
                ];
                $datas[$key]['cate'][$k]['catname'] = $v['catname'];
                $datas[$key]['cate'][$k]['total'] = Db::name($v['module'])->where($map)->count();
                $datas[$key]['cate'][$k]['url'] = url('/admin')."#/admin/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-166-178';
            }

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function member() {
        $this->title = '会员统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _member_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $vo['ids']],
            ];

            //会员
            $members = Db::name('Member')->where(['status' => 1,'is_deleted' => 0,'aid' => ['in', $vo['ids']]])->select();
            $vo['member']['total'] = count($members);
            $vo['member']['catname'] = '会员';
            $vo['member']['url'] = url('/admin').'#/admin/member/index?spm=m-220-210';

            // halt($clients);
        }
    }

    public function _ajax_get_member()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $data['ids']],
            ];

            //会员
            $members = Db::name('Member')->where(['status' => 1,'is_deleted' => 0,'aid' => ['in', $data['ids']]])->select();
            $datas[$key]['member']['total'] = count($members);
            $datas[$key]['member']['catname'] = '会员';
            $datas[$key]['member']['url'] = url('/admin').'#/admin/member/index?spm=m-220-210&aid='.$data['area_id'];

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function client() {
        $this->title = '传承人统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _client_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $vo['ids']],
            ];

            //传承人
            $clients = Db::name('Client')->where(['status' => 1,'is_deleted' => 0,'aid' => ['in', $vo['ids']]])->select();
            $vo['client']['catname'] = '传承人';
            $vo['client']['total'] = count($clients);
            $vo['client']['url'] = url('/admin').'#/admin/clientuser/index?spm=m-220-190';

            // halt($clients);
        }
    }

    public function _ajax_get_client()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $data['ids']],
            ];

            //传承人
            $clients = Db::name('Client')->where(['status' => 1,'is_deleted' => 0,'aid' => ['in', $data['ids']]])->select();
            $datas[$key]['client']['catname'] = '传承人';
            $datas[$key]['client']['total'] = count($clients);
            $datas[$key]['client']['url'] = url('/admin').'#/admin/clientuser/index?spm=m-220-190&aid='.$data['area_id'];

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function cha() {
        $this->title = '诊疗技术统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _cha_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $vo['ids']],
            ];            

            //诊疗技术
            $chas = Db::name('Characteristic')->where(['status' => 1,'aid' => ['in', $vo['ids']]])->select();
            $vo['cha']['total'] = count($chas);
            $vo['cha']['catname'] = '诊疗技术';
            $vo['cha']['url'] = url('/admin').'#/admin/characteristic/index?spm=m-206-200';

            // halt($clients);
        }
    }

    public function _ajax_get_cha()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $data['ids']],
            ];

            //诊疗技术
            $chas = Db::name('Characteristic')->where(['status' => 1,'aid' => ['in', $data['ids']]])->select();
            $datas[$key]['cha']['total'] = count($chas);
            $datas[$key]['cha']['catname'] = '诊疗技术';
            $datas[$key]['cha']['url'] = url('/admin').'#/admin/characteristic/index?spm=m-206-200&aid='.$data['area_id'];

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function drug() {
        $this->title = '药品统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _drug_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $vo['ids']],
            ];

            //药品
            $drugs = Db::name('Drug')->where(['status' => 1,'aid' => ['in', $vo['ids']]])->select();
            $vo['drug']['catname'] = '药品';
            $vo['drug']['total'] = count($drugs);
            $vo['drug']['url'] = url('/admin').'#/admin/drug/index?spm=m-208-202';

            // halt($clients);
        }
    }

    public function _ajax_get_drug()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);
            $map = [
                // 'is_deleted' => '0',
                // 'status' => 1,
                'aid' => ['in', $data['ids']],
            ];

            //药品
            $drugs = Db::name('Drug')->where(['status' => 1,'aid' => ['in', $data['ids']]])->select();
            $datas[$key]['drug']['catname'] = '药品';
            $datas[$key]['drug']['total'] = count($drugs);
            $datas[$key]['drug']['url'] = url('/admin').'#/admin/drug/index?spm=m-208-202&aid='.$data['area_id'];

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function article() {
        $this->title = '资讯统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _article_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);

            //栏目
            foreach ($this->categorys2 as $k => $v) {
                // halt($vo['ids']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $vo['ids']],
                ];
                $vo['news'][$v['id']]['catname'] = $v['catname'];
                $vo['news'][$v['id']]['total'] = Db::name($v['module'])->where($map)->count();
                $vo['news'][$v['id']]['url'] = url('/admin')."#/admin/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-166-178';
            }

            // halt($vo);
        }
    }

    public function _ajax_get_article()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);

            //栏目
            foreach ($this->categorys2 as $k => $v) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $data['ids']],
                ];
                $datas[$key]['news'][$k]['catname'] = $v['catname'];
                $datas[$key]['news'][$k]['total'] = Db::name($v['module'])->where($map)->count();
                $datas[$key]['news'][$k]['url'] = url('/admin')."#/admin/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-166-178';
            }

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

    /**
     * 显示地区列表
     */
    public function course() {
        $this->title = '课件统计';
        $map['area_id'] = $this->aid;
        $db = $this->AR->order('area_sort asc, area_id asc')->where($map);
        // halt($data);

        return parent::_list($db, false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _course_data_filter(&$data) {
        foreach ($data as &$vo) {
            $vo['ids'] = _getAIDs($vo['area_id']);

            //栏目
            foreach ($this->categorys1 as $k => $v) {
                // halt($vo['ids']);
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $vo['ids']],
                ];
                $vo['cate'][$v['id']]['catname'] = $v['catname'];
                $vo['cate'][$v['id']]['total'] = Db::name($v['module'])->where($map)->count();
                $vo['cate'][$v['id']]['url'] = url('/admin')."#/admin/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-166-178';
            }

            // halt($clients);
        }
    }

    public function _ajax_get_course()
    {
        $map['area_parent_id'] = $this->request->param('area_parent_id'); // 默认获取一级地区
        $datas = $this->AR->order('area_sort asc, area_id asc')->where($map)->select();

        foreach ($datas as $key=>$data) {
            // 判断region是否为null
            if($data['area_region'] == null) {
                $datas[$key]['area_region'] = '';
            }
            
            $data['ids'] = _getAIDs($data['area_id']);

            //栏目
            foreach ($this->categorys1 as $k => $v) {
                $map = [
                    // 'is_deleted' => '0',
                    // 'status' => 1,
                    'catid' => $v['id'],
                    'aid' => ['in', $data['ids']],
                ];
                $datas[$key]['cate'][$k]['catname'] = $v['catname'];
                $datas[$key]['cate'][$k]['total'] = Db::name($v['module'])->where($map)->count();
                $datas[$key]['cate'][$k]['url'] = url('/admin')."#/admin/".$v['module']."/index/catid/".$v['id'].".html".'?spm=m-166-178';
            }

            // 查找是否有下级地区
            $hasChild = $this->AR->where('area_parent_id', $data['area_id'])->count();

            if($hasChild > 0){
                $datas[$key]['have_child'] = 1;
            }
        }

       return json($datas);
    }

}
